AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')
include('shared.lua')

// Locals -- Calling things here so you don't have to below.
local ProjModel 	= "models/props/de_inferno/crate_fruit_break_gib2.mdl"
local ProjEnt 		= "proj_raptorbolt"

local SplodeEffect	= "Miniblast"
local TrailTexture = "trails/plasma.vmt"

local BoomSound = "weapons/mortar/mortar_explode2.wav"

ENT.EntColor = Color(0, 0, 0, 255)

ENT.WDamage = 45
ENT.WRange	= 85
ENT.WaterDestroy = 1

ENT.TrailEnabled = 1
ENT.TrailColor = Color(200, 50, 50, 255)
ENT.TrailAdditive = false
ENT.TrailStartWidth = 15
ENT.TrailEndWidth = 1
ENT.TrailLifeTime = 1
ENT.TrailResolution = 35



// Content
function ENT:SpawnFunction(ply, tr)
	if not tr.Hit then return end

	local SpawnPos = tr.HitPos + tr.HitNormal*16
	local ent = ents.Create( ProjEnt )
	ent:SetPos(SpawnPos)
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Initialize()

	self.Entity:SetModel( ProjModel )
	self.Entity:SetColor( self.EntColor )
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)	
	self.Entity:SetSolid(SOLID_VPHYSICS)
	
	if (self.TrailEnabled == 1) then
		if SERVER then
			util.SpriteTrail(self.Entity, 0, self.TrailColor, self.TrailAdditive, self.TrailStartWidth, self.TrailEndWidth, self.TrailLifeTime, self.TrailResolution, TrailTexture)
		end
	end


	local phys = self.Entity:GetPhysicsObject()
	if phys:IsValid() then
		phys:Wake()
	end
end

function ENT:Think()
	if (self.WaterDestroy == 1) then
		if self.Entity:WaterLevel() > 0 then
			self.Entity:BlowUp()
		end
	end
end

function ENT:PhysicsCollide(data, physobj)
	self.Entity:BlowUp()
end

function ENT:BlowUp( ply )
	local ply = self.Entity:GetOwner()
	local effectdata = EffectData()
	effectdata:SetNormal( Vector(0,0,1) )
	effectdata:SetOrigin( self:GetPos() )
	util.Effect( SplodeEffect , effectdata  )
	
	for _, v in ipairs(ents.FindInSphere( self:GetPos(), self.WRange )) do
		local dmginfo = DamageInfo()
		dmginfo:SetAttacker( ply )
		dmginfo:SetInflictor( self )
		dmginfo:SetDamage( self.WDamage )
		v:TakeDamageInfo( dmginfo )
	end
	
	
    sound.Play( BoomSound , self:GetPos(), 100, math.random(100,115), 1)

	self.Entity:Remove()
end


